package com.synnex.mobile.servier.core.survey.service.impl;

import com.ibatis.sqlmap.engine.transaction.Transaction;
import com.synnex.mobile.generic.core.util.ObjectUtil;
import com.synnex.mobile.generic.core.util.StringUtil;
import com.synnex.mobile.servier.core.service.impl.AbstractServiceImpl;
import com.synnex.mobile.servier.core.survey.bo.AnswerItemBo;
import com.synnex.mobile.servier.core.survey.bo.SurveyAnswerBo;
import com.synnex.mobile.servier.core.survey.bo.SurveyBo;
import com.synnex.mobile.servier.core.survey.dao.AnswerItemDao;
import com.synnex.mobile.servier.core.survey.dao.SurveyAnswerDao;
import com.synnex.mobile.servier.core.survey.dao.SurveyDao;
import com.synnex.mobile.servier.core.survey.dao.UserAnswerDao;
import com.synnex.mobile.servier.core.survey.dto.AnswerItem;
import com.synnex.mobile.servier.core.survey.dto.Survey;
import com.synnex.mobile.servier.core.survey.dto.SurveyAnswer;
import com.synnex.mobile.servier.core.survey.dto.UserAnswer;
import com.synnex.mobile.servier.core.survey.service.SurveyService;
import org.omg.Security.Public;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: kerryf
 * Date: 13-8-9
 * To change this template use File | Settings | File Templates.
 */
@Transactional
public class SurveyServiceImpl extends AbstractServiceImpl implements SurveyService {

    private SurveyDao surveyDao;
    private AnswerItemDao answerItemDao;

    private UserAnswerDao userAnswerDao;
    private SurveyAnswerDao surveyAnswerDao;

    public void setUserAnswerDao(UserAnswerDao userAnswerDao) {
        this.userAnswerDao = userAnswerDao;
    }

    public void setSurveyAnswerDao(SurveyAnswerDao surveyAnswerDao) {
        this.surveyAnswerDao = surveyAnswerDao;
    }

    public void setAnswerItemDao(AnswerItemDao answerItemDao) {
        this.answerItemDao = answerItemDao;
    }

    public void setSurveyDao(SurveyDao surveyDao) {
        this.surveyDao = surveyDao;
    }

    public List<SurveyBo> listSurvey(Integer qid) {
        List<Survey> datas = surveyDao.listSurvey(qid);
        return SurveyBo.createInstance(datas);
    }

    public void addSurvey(SurveyBo bo) {
        Survey dto = ObjectUtil.copyProperties(bo, Survey.class);

        if (bo.getAseq() != null && bo.getAseq().length > 0) {
            String[] aseq = bo.getAseq();
            String[] aTitle = bo.getAtitle();
            Set<AnswerItem> items = new HashSet<AnswerItem>();
            for (int i = 0; i < aseq.length; i++) {
                AnswerItem answerItem = new AnswerItem();
                answerItem.setQuestion(dto);
                answerItem.setEntryDate(new Date());
                answerItem.setTitle(aTitle[i]);
                answerItem.setSeq(aseq[i]);
                items.add(answerItem);
            }
            dto.setAnswerItems(items);
        }

        dto.setEntryDate(new Date());
        surveyDao.addSurvey(dto);
    }

    public void editSurvey(SurveyBo bo) {
        Survey dto = ObjectUtil.copyProperties(bo, Survey.class);

//        answerItemDao.deleteAnswerBySurveyId(bo.getId());
        if (bo.getAtitle() != null && bo.getAtitle().length > 0) {
            String[] aseq = bo.getAseq();
            String[] aTitle = bo.getAtitle();
            String[] aid = bo.getAid();
            Set<AnswerItem> items = new HashSet<AnswerItem>();
            for (int i = 0; i < aseq.length; i++) {
                AnswerItem answerItem = new AnswerItem();
                answerItem.setQuestion(dto);
                answerItem.setUpdateDate(new Date());
                answerItem.setTitle(aTitle[i]);
                answerItem.setSeq(aseq[i]);
                if (StringUtil.isInteger(aid[i])) {
                    answerItem.setId(Integer.parseInt(aid[i]));
                }
                items.add(answerItem);
            }
            dto.setAnswerItems(items);
        }

        dto.setUpdateDate(new Date());
        surveyDao.editSurvey(dto);
    }

    public void deleteSurveyById(Integer id) {
        surveyDao.deleteSurveyById(id);
    }

    public SurveyBo getSurveyById(Integer id) {
        Survey dto = surveyDao.getSurveyById(id);

        List<SurveyAnswer> sa = surveyAnswerDao.listSABySurveyId(id);

        return SurveyBo.createInstance(dto);
    }


    public void submitSurvey(List<SurveyAnswer> surveyAnswerList, List<UserAnswer> userAnswerList) {

        if (userAnswerList != null && !userAnswerList.isEmpty()) {

            for (UserAnswer answer : userAnswerList) {
                answer.setEntryDate(new Date());
                userAnswerDao.saveUserAnswer(answer);
            }
        }

        if (surveyAnswerList != null && !surveyAnswerList.isEmpty()) {

            for (SurveyAnswer surveyAnswer : surveyAnswerList) {
                surveyAnswer.setEntryDate(new Date());
                surveyAnswerDao.saveSurveyAnswer(surveyAnswer);
            }
        }

    }

    public List<SurveyBo> listSACount(Integer qid) {
        List<Survey> datas = surveyDao.listSurvey(qid);
        List<SurveyBo> surveyBoList = SurveyBo.createInstance(datas);

        if (surveyBoList != null && !surveyBoList.isEmpty()) {
            for (SurveyBo surveyBo : surveyBoList) {
                countAnswerChooseForSurvey(surveyBo);
            }
        }
        return surveyBoList;
    }

    public Integer countTotalForQuestionnaire(Integer qid) {
        Integer count = 0;
        count = userAnswerDao.countDoSurveyTotal(qid);
        return count;

    }

    private void countAnswerChooseForSurvey(SurveyBo surveyBo) {

        List<SurveyAnswerBo> surveyAnswerBoList = surveyAnswerDao.listSACountBySid(surveyBo.getId()); //当前问题下的答案统计信息
        List<AnswerItemBo> answerItemBoList = surveyBo.getAnswerItems(); //当前问题下的备选答案列表
        List<String> askAnswers = new ArrayList<String>(); // 当前问题下的问答题答案
        int totalCount = 0;

        if (surveyAnswerBoList != null && !surveyAnswerBoList.isEmpty()) {
            for (SurveyAnswerBo surveyAnswerBo : surveyAnswerBoList) {
                totalCount+=  surveyAnswerBo.getCount().intValue();

                for (AnswerItemBo answerItemBo : answerItemBoList) {
                    if (surveyAnswerBo.getAid().intValue() == answerItemBo.getId().intValue()) {
                        answerItemBo.setChooseCount(surveyAnswerBo.getCount());
                        break;
                    }
                }
            }
        }

        if (surveyBo.getType() != null && (surveyBo.getType().equals("ask"))) {
            List<SurveyAnswer> askAnswer = surveyAnswerDao.getAskAnswerBySid(surveyBo.getId());
            if (askAnswer != null && !askAnswer.isEmpty()) {
                for (SurveyAnswer surveyAnswer : askAnswer) {
                    askAnswers.add(surveyAnswer.getaText());
                }
            }
        }

        if (surveyBo.getTitleOther() != null && !surveyBo.getTitleOther().equals("")) {
            List<SurveyAnswer> askAnswer = surveyAnswerDao.getAskAnswerBySid(surveyBo.getId());
            if (askAnswer != null && !askAnswer.isEmpty()) {
                for (SurveyAnswer surveyAnswer : askAnswer) {
                    askAnswers.add(surveyAnswer.getaText());
                }
            }
        }
        surveyBo.setAskInfoList(askAnswers);
        surveyBo.setSurveyCount(totalCount);
    }
}
